*******************************************************************************
* Load candidates data
*******************************************************************************

use dataset_candidates, clear
keep if !mi(ideology)

*******************************************************************************
* Rescale ideology and presidential vote and TrueViews
*******************************************************************************

su ideology
replace ideology = (ideology - r(min))/(r(max) - r(min))
su trump
replace trump = (trump - r(min))/(r(max) - r(min))
su support
replace support = (support - r(min))/(r(max) - r(min))

*******************************************************************************
* Make median split on ideology, Trump vote, TrueViews
*******************************************************************************

xtile qideo = ideology, nq(2)
xtile qtrump = trump, nq(2)
xtile qsupport = support, nq(2)

replace ideology = qideo - 1
replace trumpvote = qtrump - 1
replace support = qsupport - 1

*******************************************************************************
* Make interactions
*******************************************************************************

gen deputyXtrumpvoteshare = deputy*trumpvoteshare
gen ideologyXtrumpvoteshare = ideology*trumpvoteshare
gen deputyXsupport = deputy*support
gen ideologyXsupport = ideology*support

*******************************************************************************
* Do the regressions with Trump vote
*******************************************************************************

eststo clear

eststo: areg voteshare ideology trumpvoteshare ideologyXtrumpvoteshare cycle_* ///
	i.ncandidates, cl(district) a(state)	
eststo: areg voteshare deputy trumpvoteshare deputyXtrumpvoteshare cycle_* ///
	i.ncandidates, cl(district) a(state)

eststo: areg winner ideology trumpvoteshare ideologyXtrumpvoteshare cycle_* ///
	i.ncandidates, cl(district) a(state)	
eststo: areg winner deputy trumpvoteshare deputyXtrumpvoteshare cycle_* ///
	i.ncandidates, cl(district) a(state)

*******************************************************************************
* Output
*******************************************************************************

label variable ideology "Candidate Conservatism"
label variable deputy "Experienced"
label variable incumbent "Incumbent"
label variable trumpvoteshare "Trump Vote Share"
label variable deputyXtrumpvoteshare "Experienced X Trump Vote"
label variable ideologyXtrumpvoteshare "Cand. Conservatism X Trump Vote"

#delimit;

esttab
	using "_output/tableS10_a.tex"
	,
		replace
		booktabs
		cells(b(fmt(3)) se(par) p(par("[" "]")))
		order(deputy ideology)
		drop(cycle* *cand* _cons) 
		stats(
			N,
			label(
				"Observations"
			)
			fmt(%20.0f %20.0f)
		)
		label
		collabels(none)	
		mlabels(none)
		width(\linewidth)
		alignment(l)
		gap
		mgroups(
			"DV=Vote Share" "DV=Elected", pattern(1 0 1 0) 
			prefix(\multicolumn{@span}{c}{) suffix(}) 
			span 
			erepeat(\cmidrule(lr){@span})
		)
		;
		
#delimit cr

*******************************************************************************
* Do the regressions with TrueViews 
*******************************************************************************

eststo clear

eststo: areg voteshare ideology support ideologyXsupport cycle_* ///
	i.ncandidates, cl(district) a(state)	
eststo: areg voteshare deputy support deputyXsupport cycle_* ///
	i.ncandidates, cl(district) a(state)

eststo: areg winner ideology support ideologyXsupport cycle_* ///
	i.ncandidates, cl(district) a(state)	
eststo: areg winner deputy support deputyXsupport cycle_* ///
	i.ncandidates, cl(district) a(state)

*******************************************************************************
* Output
*******************************************************************************

label variable support "Constituency Conservatism"
label variable deputyXsupport "Experienced X Const. Conservatism"
label variable ideologyXsupport "Cand. Conservatism X Const. Conservatism"

#delimit;

esttab
	using "_output/tableS10_b.tex"
	,
		replace
		booktabs
		cells(b(fmt(3)) se(par) p(par("[" "]")))
		order(deputy ideology)
		drop(cycle* *cand* _cons) 
		stats(
			N,
			label(
				"Observations"
			)
			fmt(%20.0f %20.0f)
		)
		label
		collabels(none)	
		mlabels(none)
		width(\linewidth)
		alignment(l)
		gap
		mgroups(
			"DV=Vote Share" "DV=Elected", pattern(1 0 1 0) 
			prefix(\multicolumn{@span}{c}{) suffix(}) 
			span 
			erepeat(\cmidrule(lr){@span})
		)
		;
		
#delimit cr

*******************************************************************************
* End
*******************************************************************************
